﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DBService;


namespace AccessDBService
{
    public class AccessDBServiceClass:IDBService
    {
        #region IDBService 成员

        System.Data.DataTable IDBService.Select(string sql, string conectionString)
        {
            DataTable dt = new DataTable();

            OleDbCommand cmd = new OleDbCommand(sql, new OleDbConnection(conectionString));
            OleDbDataAdapter adt = new OleDbDataAdapter(cmd);
            adt.Fill(dt);
            cmd.Connection.Close();
            return dt;
        }

        object IDBService.ExcecuteScalar(string sql, string conectionString)
        {

            OleDbCommand cmd = new OleDbCommand(sql, new OleDbConnection(conectionString));
            cmd.Connection.Open();
            object bb = cmd.ExecuteScalar();
            cmd.Connection.Close();
            cmd.Dispose();
            return bb;
        }

        int IDBService.ExecuteNonQuery(string sql, string conectionString)
        {
            OleDbCommand cmd = new OleDbCommand(sql, new OleDbConnection(conectionString));
            cmd.Connection.Open();
            int bb = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            cmd.Dispose();
            return bb;
        }






        string IDBService.ToPaginatedSql(string sql, long rows, long pageIndex)
        {
            throw new NotImplementedException();
            /*
            string selectString=sql;
            long page = pageIndex;
            string pageStr = "";
            string rownumberColumnName = "myrowsx";
            if (page != 0)
            {

                pageStr = string.Format("and ({2}>{0} and {2}<={1})", rows * (page - 1), rows * page, rownumberColumnName);
            }
            string temp = selectString.ToLower();

            int index = temp.IndexOf("select") + 6;
            temp = selectString.Substring(index, selectString.Length - index);
            return string.Format("select * from(select (ROW_NUMBER() OVER({3})) as {0} , {1} )cc where 1=1 {2}", rownumberColumnName, temp, pageStr, getOrderCondition(temp));
             * */
        }
        public string getOrderCondition(string selectStr)
        {
            throw new NotImplementedException();

            string temp = selectStr.ToLower();
            int index = temp.IndexOf("order");
            if (index < 0) return "";
            return selectStr.Substring(index, selectStr.Length - index);
        }

        string IDBService.ToCountedSql(string sql)
        {
            /*
            string selectString = sql;
            string temp = selectString.ToLower();

            int index = temp.IndexOf("from") + 4;

            selectString = selectString.Substring(index, selectString.Length - (index + 1));
            temp = selectString.ToLower();
            index = temp.IndexOf("order");
            if (index != -1) selectString = selectString.Substring(0, index);

            return string.Format("select count(*) from {0} ", selectString);
              */
            return string.Format("select count(*) from ({0}) as mycounttable ", sql);
        }

      


        DataTable IDBService.ExcecuteStoredProcedure(string conectionString, string procedure_name, DBParameterCollentions paras)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
